package org.mms.controller;

import org.mms.pojo.dto.MaterialInboundDto;
import org.mms.pojo.dto.MaterialOutboundDto;
import org.mms.pojo.entity.MaterialOutbound;
import org.mms.pojo.request.MaterialInboundRequest;
import org.mms.pojo.request.MaterialOutboundRequest;
import org.mms.pojo.request.MaterialOutboundReviewRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.OutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/material-outbounds")
public class OutboundController {
    @Autowired
    private OutboundService outboundService;

    @GetMapping
    public ResponseMessage<List<MaterialOutboundDto>> list() {
        return ResponseMessage.success(outboundService.list()
                .stream()
                .map(MaterialOutbound::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<MaterialOutboundDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(outboundService.getById(id)
                .convert());
    }

    @PostMapping
    public ResponseMessage<MaterialOutboundDto> create(@RequestBody MaterialOutboundRequest request) {
        return ResponseMessage.success(outboundService.create(request)
                .convert());
    }

    @PutMapping()
    public ResponseMessage<MaterialOutboundDto> update(@RequestBody MaterialOutboundRequest request) {
        return ResponseMessage.success(outboundService.update(request)
                .convert());
    }

    @PutMapping("/review")
    public ResponseMessage<MaterialOutboundDto> review(@RequestBody MaterialOutboundReviewRequest request) {
        return ResponseMessage.success(outboundService.review(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        outboundService.delete(id);
        return ResponseMessage.success(null);
    }
}